﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HNCSolutions.Models;
using HNCSolutions.ViewModel;

namespace HNCSolutions.Areas.Admin.Controllers
{
    public class WebsiteController : Controller
    {
        private HNCSolutionsEntities db = new HNCSolutionsEntities();
        //
        // GET: /Admin/Website/

        public ActionResult Index(int currentPage = 1, int pageRows = 2)
        {
            var listWebSite = db.Websites.OrderBy(ws => ws.Id).Skip((currentPage - 1) * pageRows).Take(pageRows);
            int totalPages = (db.Websites.Count() / pageRows) + (db.SanPhams.Count() % pageRows > 0 ? 1 : 0);
            ListWebsiteViewModel viewModel = new ListWebsiteViewModel
            {
                ListWebsite = listWebSite,
                Paging = new Paging { CurrentPage = currentPage, PageRows = pageRows, TotalPage = totalPages }
            };

            return View(viewModel);
        }       

        //
        // GET: /Admin/Website/Create
        public ActionResult Create()
        {
            WebsiteViewModel viewModel = new WebsiteViewModel();
            viewModel.Website = new Website();
            return View(viewModel);
        }

        #region Create & Update website
        //
        // POST: /Admin/Website/Create

        [HttpPost]
        public ActionResult Create(WebsiteViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                db.Websites.Add(viewModel.Website);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(viewModel);
        }

        //
        // GET: /Admin/Website/Edit/5

        public ActionResult Edit(int id = 0)
        {
            WebsiteViewModel viewModel = new WebsiteViewModel();
            Website website = db.Websites.Find(id);
            viewModel.Website = website;
            if (website == null)
            {
                return HttpNotFound();
            }
            return View(website);
        }

        //
        // POST: /Admin/Website/Edit/5

        [HttpPost]
        public ActionResult Edit(Website website)
        {
            if (ModelState.IsValid)
            {
                db.Entry(website).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(website);
        }
        #endregion
        

        //
        // GET: /Admin/Website/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Website website = db.Websites.Find(id);
            if (website == null)
            {
                return HttpNotFound();
            }
            return View(website);
        }

        //
        // POST: /Admin/Website/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Website website = db.Websites.Find(id);
            db.Websites.Remove(website);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}